﻿using FlexFlow.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace FlexFlow.Runtime
{
    /// <summary>
    /// 工作流表单管理器
    /// </summary>
    public class WorkFlowFormManager : ManagerBase
    {
        /// <summary>
        /// 获取或设置默认的工作流表单管理器
        /// </summary>
        public static WorkFlowFormManager Default { get; set; }
        /// <summary>
        /// 重命名工作流表单
        /// </summary>
        /// <param name="id">
        /// 要重命名的表单ID
        /// </param>
        /// <param name="newName">
        /// 表单的新名称
        /// </param>
        /// <returns>
        /// 操作结果
        /// </returns>
        public OperateResult Rename(Guid id, string newName)
        {
            if (!this.Components.WorkFlowFormProvider.Rename(id, newName))
            {
                return new OperateResult()
                {
                    ResultCode = 0x070201,
                    Message = "重命名失败！"
                };
            }

            return OperateResult.SuccessResult();
        }
        /// <summary>
        /// 创建一个工作流表单
        /// </summary>
        /// <param name="name">
        /// 表单名称
        /// </param>
        /// <param name="templateId">
        /// 表单模板ID
        /// </param>
        /// <param name="creator">
        /// 表单的创建者ID
        /// </param>
        /// <returns>
        /// 操作结果
        /// </returns>
        public OperateResult Create(string name, Guid templateId, Guid creator)
        {
            if (this.Components.WorkFlowFormProvider.Get(name, templateId, creator) != null)
            {
                return new OperateResult()
                {
                    ResultCode = 0x070301,
                    Message = "指定类型的具有指定名称的表单已经存在"
                };
            }
            if (!this.Components.UserProvider.Exist(creator))
            {
                return new OperateResult()
                {
                    ResultCode = 0x070302,
                    Message = "用户ID无效！"
                };
            }
            if (!this.Components.FormTemplateProvider.Exist(templateId))
            {
                return new OperateResult()
                {
                    ResultCode = 0x070303,
                    Message = "表单模板ID无效！"
                };
            }

            WorkFlowForm frm = this.Components.WorkFlowFormProvider.Create();
            frm.Name = name;
            frm.Template = templateId;
            frm.Creator = creator;

            if (!this.Components.WorkFlowFormProvider.Add(frm))
            {
                return new OperateResult()
                {
                    ResultCode = 0x070304,
                    Message = "数据加入失败！"
                };
            }

            return OperateResult.SuccessResult(frm.Identity);
        }
        /// <summary>
        /// 更新指定ID对应的表单模板的内容
        /// </summary>
        /// <param name="frmId">
        /// 表单模板ID
        /// </param>
        /// <param name="content">
        /// 表单内容
        /// </param>
        /// <returns>
        /// 操作结果
        /// </returns>
        public OperateResult UpdateContent(Guid frmId, Dictionary<string, Object> content)
        {
            if (!this.Components.WorkFlowFormProvider.UpdateContent(frmId, content))
            {
                return new OperateResult()
                {
                    ResultCode = 0x070101,
                    Message = "数据更新失败"
                };
            }

            return OperateResult.SuccessResult();
        }
        /// <summary>
        /// 取得指定用户创建的表单列表
        /// </summary>
        /// <param name="uid">
        /// 用户ID
        /// </param>
        /// <param name="nameLike">
        /// 要取得的表单名称中应包含的字符列表
        /// </param>
        /// <param name="skip">
        /// 返回的数据的起始位置
        /// </param>
        /// <param name="formTemp">
        /// 表单模板
        /// </param>
        /// <param name="limit">
        /// 返回的数据数量限制
        /// </param>
        /// <returns>
        /// 操作结果
        /// </returns>
        public OperateResult GetUserForms(Guid uid, string nameLike = "", Guid? formTemp = null, int skip = 0, int limit = 10)
        {
            return OperateResult.SuccessResult(this.Components.WorkFlowFormProvider.GetUserForms(uid, nameLike, formTemp, skip, limit));
        }
        /// <summary>
        /// 取得表单
        /// </summary>
        /// <param name="formId">表单ID</param>
        /// <param name="containContent">
        /// 一个值，表示是否包含表单内容
        /// </param>
        /// <returns>
        /// 操作结果
        /// </returns>
        public OperateResult GetById(Guid formId,bool containContent=true)
        {
            WorkFlowForm form = this.Components.WorkFlowFormProvider.Get(formId);
            if (form == null)
            {
                return new OperateResult()
                {
                    ResultCode = 0x070401,
                    Message = "指定ID对应的表单不存在！"
                };
            };
            if (!containContent)
            {
                form.Content = null;
            }
            return OperateResult.SuccessResult(form);
        }
        /// <summary>
        /// 移除表单
        /// </summary>
        /// <param name="formId">
        /// 表单ID
        /// </param>
        /// <returns>
        /// 操作结果
        /// </returns>
        public OperateResult Remove(Guid formId)
        {
            if (!this.Components.WorkFlowFormProvider.Delete(formId))
            {
                return new OperateResult()
                {
                    ResultCode = 0x070501,
                    Message = "移除表单失败！"
                };
            }
            return OperateResult.SuccessResult();
        }
        static WorkFlowFormManager()
        {
            Default = new WorkFlowFormManager();
        }
    }
}
